package xing.service.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import xing.application.api.OwnerRequireApi;
import xing.application.api.wechat.UserAuth;
import xing.infrastructure.data.CallResult;

import javax.servlet.http.HttpServletRequest;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date： 2017-10-16 14:51
 * @Description:
 * @version 1.0.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0.0     2017-10-16    Administrator                            
 *******************************************************************************************
 */
@Api(value = "业主报事",description = "")
@RestController
@RequestMapping("/req")
public class OwnerReqController {
    @Autowired
    OwnerRequireApi ownerRequireApi;

    @Autowired
    UserAuth userAuth;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiOperation(value="报事列表", notes="")
    @ApiImplicitParams({@ApiImplicitParam(name = "searchText", value = "搜索文字", dataType = "String", paramType = "query" ),
            @ApiImplicitParam(name = "searchType", value = "搜索类型 (finished，unfinish，unstart，indo，expire)", dataType = "String", paramType = "query" ),
            @ApiImplicitParam(name = "compositeName", value = "排序名称 (startTime,endTime)", dataType = "String", paramType = "query" ),
            @ApiImplicitParam(name = "compositeType", value = "排序类型 （desc，asc）", dataType = "String", paramType = "query" )
    })
    @GetMapping("/list/{phone}")
    public String getReqList(@PathVariable String phone, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(defaultValue = "") String searchText,@RequestParam(defaultValue = "") String searchType,@RequestParam(defaultValue = "") String compositeName,@RequestParam(defaultValue = "") String compositeType){

        String res;
        try {
            res = ownerRequireApi.getOwnerRequireList(phone,pageNo,pageSize,searchText,searchType,compositeName,compositeType);
        } catch (Exception e) {
            logger.error("获取报事列表失败 ",e);
            res = JSON.toJSONString( new CallResult(-1,"获取报事列表失败",null));
        }
        return res;
    }

    @ApiOperation(value="报事详情", notes="")
    @GetMapping("/detail/{id}/{phone}")
    public String getReqList(@PathVariable int id,@PathVariable String phone){

        String res;
        try {
            res = ownerRequireApi.getOwnerRequireDetail(id,phone);
        } catch (Exception e) {
            logger.error("获取报事详情失败 ",e);
            res = JSON.toJSONString( new CallResult(-1,"获取报事详情失败",null));
        }
        return res;
    }

    @ApiOperation(value="报事状态数量", notes="")
    @GetMapping("/status/{phone}")
    public String getReqStatus(@PathVariable String phone){

        String res;
        try {
            res = ownerRequireApi.getOwnerRequireStatus(phone);
        } catch (Exception e) {
            logger.error("获取报事状态失败 ",e);
            res = JSON.toJSONString( new CallResult(-1,"获取报事状态失败",null));
        }
        return res;
    }

    @ApiOperation(value="已开通应用", notes="")
    @ApiResponses({
        @ApiResponse(code=401,message="tevent:报事, fhouse:业主登记, intelligent:智能管家（0-未开通   1-开通）")
      })
    @GetMapping("/openservice/{phone}")
    public String getOpenService(@PathVariable String phone){

        String res;
        try {
            res = ownerRequireApi.getOpenService(phone);
        } catch (Exception e) {
            logger.error("获取已开通应用失败 ",e);
            res = JSON.toJSONString( new CallResult(-1,"获取已开通应用失败",null));
        }
        return res;
    }

    //@GetMapping("/login")
    public CallResult login(@RequestParam String code,@RequestParam String encryptedData,@RequestParam String iv){

        String sessionKey=userAuth.getSessionKey(code);
        return CallResult.ok();
    }
}
